﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using YiSha.Util;
using YiSha.Util.Model;
using YiSha.Entity;
using YiSha.Model;
using YiSha.Admin.Web.Controllers;
using YiSha.Entity.OrganizationManage;
using YiSha.Business.OrganizationManage;
using YiSha.Model.Param.OrganizationManage;
using YiSha.Web.Code;

namespace YiSha.Admin.Web.Areas.OrganizationManage.Controllers
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2023-04-11 22:01
    /// 描 述：租户表控制器类
    /// </summary>
    [Area("OrganizationManage")]
    public class SysTenantController :  BaseController
    {
        private SysTenantBLL sysTenantBLL = new SysTenantBLL();

        private UserBLL userBLL = new UserBLL();
        #region 视图功能
        [AuthorizeFilter("organization:systenant:view")]
        public ActionResult SysTenantIndex()
        {
            return View();
        }

        public ActionResult SysTenantForm()
        {
            return View();
        }
        #endregion

        #region 获取数据
        [HttpGet]
        [AuthorizeFilter("organization:systenant:search")]
        public async Task<ActionResult> GetListJson(SysTenantListParam param)
        {
            OperatorInfo operatorInfo = await Operator.Instance.Current();
            if (operatorInfo.IsSystem == 0)
            {
                param.Id = operatorInfo.tenant_id;
            }
            TData<List<SysTenantEntity>> obj = await sysTenantBLL.GetList(param);
            return Json(obj);
        }

        [HttpGet]
        [AuthorizeFilter("organization:systenant:search")]
        public async Task<ActionResult> GetPageListJson(SysTenantListParam param, Pagination pagination)
        {
            OperatorInfo operatorInfo = await Operator.Instance.Current();
            if (operatorInfo.IsSystem == 0)
            {
                param.Id = operatorInfo.tenant_id;
            }
            TData<List<SysTenantEntity>> obj = await sysTenantBLL.GetPageList(param, pagination);
            return Json(obj);
        }

        [HttpGet]
        public async Task<ActionResult> GetFormJson(long id)
        {
            TData<SysTenantEntity> obj = await sysTenantBLL.GetEntity(id);
            return Json(obj);
        }

        /// <summary>
        /// 查询未绑定的客户账号列表
        /// </summary>
        /// <param name="param"></param>
        /// <param name="pagination"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetCustomerPageListJson(UserListParam param, Pagination pagination)
        {
            TData<List<UserEntity>> obj = await userBLL.GetCustomerPageListJson(param, pagination);
            return Json(obj);
        }
        #endregion

        #region 提交数据
        [HttpPost]
        [AuthorizeFilter("organization:systenant:add,organization:systenant:edit")]
        public async Task<ActionResult> SaveFormJson(SysTenantEntity entity)
        {
            TData<string> obj = await sysTenantBLL.SaveForm(entity);
            return Json(obj);
        }

        [HttpPost]
        [AuthorizeFilter("organization:systenant:delete")]
        public async Task<ActionResult> DeleteFormJson(string ids)
        {
            TData obj = await sysTenantBLL.DeleteForm(ids);
            return Json(obj);
        }
        #endregion
    }
}
